<template>
	<view>
		<uni-forms ref="form" :rules="{}">
			<uni-forms-item label="姓名" name="">
				<uni-easyinput v-model="user.username" placeholder="请输入姓名" />
			</uni-forms-item>
			<uni-forms-item label="密码" name="">
				<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
			</uni-forms-item>
			
			<uni-forms-item label="" name="">
				<button type="primary" @click="register" style="margin: 10px;">注册</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				user: {
					username: '',
					password: '',
				}
			}
		},
		methods: {
			register(from) {
				if(this.user.username&&this.user.password!==null){
					uni.request({
					    url: this.$baseUrl+'user/register',
					    method: 'POST',
					    data: this.user,
					    success: res => {
					        if (res.data && res.data.data && res.data.data.username) {
					            console.log(res.data);
					            uni.showToast({
					                title: '注册成功',
					                icon: 'success',
					                duration: 2000,
					                complete() {
					                  setTimeout(function () {
					                   uni.navigateTo({
					                   	url:"login"
					                   })
					                  }, 2000);
					                }
					              });
					        } else {
					            uni.showToast({
					                title: '用户名或密码错误',
					                icon: 'error'
					            });
					        }
					    },
					});
				}else{
					uni.showToast({
					    title: '用户名或密码不能为空',
					    icon: 'error'
					});
				}
			   
			    this.$refs.form.validate().then(res => {
			        console.log('表单数据信息：', res);
			    }).catch(err => {
			        console.log('表单错误信息：', err);
			    })
			}
		}
	}
</script>

<style>

</style>